﻿
define(["require", "exports",  "core/Functions.module"], function (require, exports, Functions) {
    "use strict";
    var WidgetUse = (function () {
        function WidgetUse(WidgetConfig) {
            this.WidgetConfig = WidgetConfig;
            this.panels = new Array();
            this.halfpanels = new Array();
        }
      
        WidgetUse.prototype.useWidget = function (widgetId) {
            $.each(this.WidgetConfig, function (index, item) {
            	$.each(item.data,function(index,item){
                if (item.id == widgetId) {
                    var target = new Object();
                    target.data = new Object();
                    target.data["id"] = item["id"];
                    target.data["label"] = item["label"];
                    target.data["widget"] = item["widget"];
                    target.data["main"] = item["main"];
                    target.data["in-panel"] = item["inPanel"];
                    target.data["panel"] = item["panel"];
                    var paneltype = item["panel"];
                    var panel = null;
                    if(paneltype=="HalfPanel")
                    panel = this.halfpanels[target.data["id"]];
                    else
                      panel = this.panels[target.data["id"]];
                    if (panel) {
                       // this.bug('模块[' + target.data["label"] + ']正在使用当中!');
                        if (panel.focus) {
                            panel.focus();
                        }
                        return;
                    }
                    this.loadWidget(target);
                }
               }.bind(this));
            }.bind(this));
        };
       
        WidgetUse.prototype.loadWidget = function (target) {
            var _this = this;
            if (target.data["in-panel"] === true) {
              if (target.data['panel'] === "HalfPanel") {
                    require({}, ["widgets/HalfPanel/widget"], function (HalfPanel) {
                        //_this.log('加载面板模块:' + target.data('widget'));
                        _this.halfpanels[target.data['id']] = new HalfPanel({
                             widgetPath: "widgets/HalfPanel",
                            afterDestroyCallback: _this.destroyWidget.bind(_this, target.data['id'])
                        }, {
                            panelTitle: target.data['label'],
                            panelUniqClassName: target.data['id'],
                            innerWidgetPath: target.data['widget'],
                            innerWidgetMain: target.data['main']
                        });
                    });
                }
                else {
                    require({}, ["widgets/Panel/widget"], function (Panel) {
                        //_this.log('加载面板模块:' + target.data['widget']);
                        _this.panels[target.data['id']] = new Panel({
                             widgetPath: "widgets/Panel",
                            afterDestroyCallback: _this.destroyWidget.bind(_this, target.data['id'])
                        }, {
                            panelTitle: target.data['label'],
                            panelUniqClassName: target.data['id'],
                            innerWidgetPath: target.data['widget'],
                            innerWidgetMain: target.data['main']
                        });
                    });
                }
            }
            else {
                require({}, [Functions.concatUrl(target.data["widget"], target.data["main"])], function (Widget) {
                    //_this.log('加载模块:' + target.data["widget"]);
                    _this.panels[target.data["id"]] = new Widget({
                         widgetPath: target.data["widget"],
                        afterDestroyCallback: _this.destroyWidget.bind(_this, target.data["id"])
                    });
                });
            }
        };
         WidgetUse.prototype.closeWidget = function(widgetID){
         	var panels = this.panels.concat(this.halfpanels);
             if (panels[widgetID] !== null && panels[widgetID] !== undefined)
             {
                panels[widgetID].close();
                this.destroyWidget(widgetID);
             }
           };
         WidgetUse.prototype.getWidget=function(widgetID)
           {
           	 var panels = this.panels.concat(this.halfpanels);
             var panel = panels[widgetID];
              return panel;
         };
        WidgetUse.prototype.destroyWidget = function (widgetID) {
           
            this.panels[widgetID] = null;
            delete this.panels[widgetID];
             this.halfpanels[widgetID] = null;
            delete this.halfpanels[widgetID];
           // this.log('已销毁对象:ID = ' + widgetID);
        };
        WidgetUse.prototype.addToDom = function (data) {
            var htmlString = _.template(this.template)(data);
            this.setHtml(htmlString);
        }
        return WidgetUse;
    }());
    return WidgetUse;
});
